package org.api.free.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.api.free.po.PreprocessingOptions;
import org.api.free.service.ocr.OcrService;
import org.api.free.service.ocr.OcrServiceHight;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/v1")
@CrossOrigin(origins = {"http://localhost", "http://127.0.0.1", "http://47.107.136.232"}) // 允许指定域名
@Slf4j
public class OcrController {

    @Autowired
    private OcrService ocrService;

    @Autowired
    private OcrServiceHight ocrServiceHight;

//    @PostMapping("/ocr")
    public String processOcr(@RequestParam("file") MultipartFile file) {
        try {
            log.info("收到请求");
            String ret = ocrService.recognizeTextCmd(file);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", 200);
            jsonObject.put("msg", "OK");

            JSONObject data = new JSONObject();
            data.put("text", ret);
            jsonObject.put("data", data);

            return jsonObject.toString();
        } catch (Exception e) {
            log.error("OCR失败", e);
            return "OCR失败: " + e.getMessage();
        }
    }

    @PostMapping("/ocrfast")
    public String processFastOcr(@RequestParam("file") MultipartFile file, //
                                 @RequestParam(value = "grayscale", required = false, defaultValue = "false") Boolean grayscale, //
                                 @RequestParam(value = "contrast", required = false, defaultValue = "0") Integer contrast, //
                                 @RequestParam(value = "binarization", required = false, defaultValue = "false") Boolean binarization, //
                                 @RequestParam(value = "denoising", required = false, defaultValue = "false") Boolean denoising, //
                                 @RequestParam(value = "enhanceResolution", required = false, defaultValue = "false") Boolean enhanceResolution) { //
        try {
            log.info("收到请求");

            // 创建 PreprocessingOptions 对象并设置值
            PreprocessingOptions options = new PreprocessingOptions();
            options.setGrayscale(grayscale);
            options.setContrast(contrast);
            options.setBinarization(binarization);
            options.setDenoising(denoising);
            options.setEnhanceResolution(enhanceResolution);

            String ret = ocrServiceHight.recognizeText(file,options);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", 200);
            jsonObject.put("msg", "OK");

            JSONObject data = new JSONObject();
            data.put("text", ret);
            jsonObject.put("data", data);

            return jsonObject.toString();
        } catch (Exception e) {
            log.error("OCR失败", e);
            return "OCR失败: " + e.getMessage();
        }
    }
}